/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package xdxn.editor.studio.utils;

import java.util.ArrayList;


/**
 *
 * @author Cai Han
 */
public class IDFactory {
    
    private int _id_seed;
    
    private final ArrayList<Integer> _id_active;
    private final ArrayList<Integer> _id_free;
    
    public IDFactory(){
        this._id_active = new ArrayList<>();
        this._id_free = new ArrayList<>();
    }
    
    public int requestID(){
        int size = this._id_free.size();
        int id;
        if(size > 0){
            id = this._id_free.get(0);
            this._id_free.remove(0);
            this._id_active.add(id);
        }else{
            while(true){
                if(this._id_active.contains(this._id_seed)){
                    this._id_seed++;
                }else break;
            }
            id = this._id_seed;
            this._id_active.add(id);
        }
        return id;
    }
    
    public void removeID(int id){
        if(this._id_active.contains(id)){
            this._id_active.remove(id);
            this._id_free.add(id);
        }
    }
    
}
